package feri.pkg;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import feri.baza.DBHelper;

public class PrikazNaZemljeviduActivity extends MapActivity {

	MapView mapView;
	MapController mc;
	GeoPoint p;
	List<Overlay> listOfOverlays;
	Context kontekst;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prikaznazemljevidu);
		kontekst = this;
		
		double x = getIntent().getDoubleExtra("x", 0);
		double y = getIntent().getDoubleExtra("y", 0);
		mapView = (MapView) findViewById(R.id.mapview);
		mc = mapView.getController();
		setControls();
		setPosition();
		// prikaz pin-a na mapi
		//1E6
		GeoPoint p = new GeoPoint((int) (x * 1E6), (int) (y * 1E6));
		mc.animateTo(p);
		mc.setZoom(16);
		MyoverlayClass mapOverlay = new MyoverlayClass(p,false);
		
		listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);
		mapView.invalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	protected void setControls() {

		mapView.showContextMenu();
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(true);

	}

	private void setPosition() {
		// TODO Auto-generated method stub

		LocationManager lcm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Context context = getApplicationContext();

		LocationListener mlocListener = new MojLocationListener(context, mc);
		lcm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				mlocListener);

		// GeoPoint point2 = new GeoPoint(35410000, 139460000);
		// mc.animateTo(point2);

	}
	
	
	
	
	public class MojLocationListener implements LocationListener {
		
		Context mapContext;
		//MapController mc;
		
		
		public MojLocationListener(Context context,MapController _mc) {
			super();
			// TODO Auto-generated constructor stub
			mapContext=context;
			//mc=_mc;
			
		}



		public void onLocationChanged(Location loc)
		{
			loc.getLatitude();
			loc.getLongitude();
			String Text = "Vaša lokacija: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
			//Toast.makeText(mapContext,Text,Toast.LENGTH_SHORT).show();
			//Toast.makeText(mapContext,Text,Toast.LENGTH_SHORT).show();
					
			GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));
			
			mc.animateTo(point);
			mc.setZoom(16);
			System.out.println("Velikost "+listOfOverlays.size());
			if(listOfOverlays.size()>1){
				listOfOverlays.remove(1);
			}
			
			
			
			MyoverlayClass mapOverlay = new MyoverlayClass(point,true);
			
			listOfOverlays = mapView.getOverlays();
			
			Drawable icon1 = getResources().getDrawable(R.drawable.reddot);
			
			
			listOfOverlays.add(mapOverlay);
			mapView.invalidate();
			
		}

		

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			Toast.makeText( mapContext,"Gps Disabled",Toast.LENGTH_SHORT).show();
			
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			Toast.makeText( mapContext,"Gps Enabled",Toast.LENGTH_SHORT).show();
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			Toast.makeText( mapContext,"Gps status changed",Toast.LENGTH_SHORT).show();
			
		}

	}
}
